package garbage;

import com.asteros.common.util.Base64Util;
import com.asteros.common.util.io.IOUtil;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.PublicKey;
import java.security.cert.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public final class CertificateUtil
{
  private static final String BEGIN_CERT = "-----BEGIN CERTIFICATE-----\r\n";
  private static final String END_CERT = "-----END CERTIFICATE-----\r\n";
  private static final String UTF_16LE = "UTF-16LE";

  public static PublicKey getPublicKey(Certificate cert)
  {
    return cert.getPublicKey();
  }

  public static Certificate getX509CertificateFromBase64(InputStream stream)
    throws CertificateException, IOException
  {
    String base64Str = Base64Util.toBase64String(new String(IOUtil.readFully(stream)), "-----BEGIN CERTIFICATE-----\r\n", "-----END CERTIFICATE-----\r\n");
    Certificate certificate = CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(base64Str.getBytes()));
    return certificate;
  }

  public static Certificate getX509Certificate(InputStream stream) throws CertificateException
  {
    Certificate certificate = CertificateFactory.getInstance("X.509").generateCertificate(stream);
    return certificate;
  }

  public static Certificate[] getX509CertificatesFromPKCS7(InputStream stream) throws CertificateException, Exception
  {
    Certificate[] certs = null;

    byte[] bytes = IOUtil.readFully(stream);

    int beginLength = "-----BEGIN CERTIFICATE-----\r\n".length();
    int endLength = "-----END CERTIFICATE-----\r\n".length();

    String beginEnd = new String(bytes, "UTF-16LE");
    String clear = beginEnd.substring(beginLength, beginEnd.length() - endLength);

    /*Decoder decoder = new Decoder();
    byte[] encoded = decoder.decodeBuffer(new ByteArrayInputStream(clear.getBytes()));
    certs = getCertsChain(encoded);*/

    return certs;
  }

  public static Certificate[] getCertsChain(byte[] buf) throws Exception {
    /*Asn1BerDecodeBuffer asnBuf = new Asn1BerDecodeBuffer(buf);
    ContentInfo sd = new ContentInfo();
    sd.decode(asnBuf);
    Certificate[] certificates = new Certificate[((SignedData)sd.content).certificates.elements.length];
    Asn1BerEncodeBuffer encBuf = new Asn1BerEncodeBuffer();
    for (int i = 0; i < ((SignedData)sd.content).certificates.elements.length; i++) {
      ((SignedData)sd.content).certificates.elements[i].encode(encBuf);
      CertificateFactory cf = CertificateFactory.getInstance("X.509");
      Certificate cert = cf.generateCertificate(encBuf.getInputStream());
      certificates[i] = cert;
    }
    return certificates;*/

      return null;
  }

  public static X509CRL getX509CRL(InputStream in)
    throws Exception
  {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509CRL crl = null;
    try {
      crl = (X509CRL)cf.generateCRL(in);
    } finally {
      if (in != null) in.close();
    }
    return crl;
  }

  public static X509CRL getX509CRLFromBase64(InputStream in) throws Exception
  {
    /*byte[] buf = IOUtil.readFully(in);
    Decoder decoder = new Decoder();
    byte[] cmsDer = decoder.decodeBuffer(new ByteArrayInputStream(buf));
    X509CRL crls = getX509CRL(new ByteArrayInputStream(cmsDer));
    return crls;*/
      return null;
  }

  public static List<BigInteger> getCRLSerialNumbers(InputStream in) throws Exception
  {
    List list = new ArrayList();
    X509CRL crls = getX509CRL(in);
    if (crls != null)
    {
      X509CRLEntry entry;
      for (Iterator i$ = crls.getRevokedCertificates().iterator(); i$.hasNext(); list.add(entry.getSerialNumber())) entry = (X509CRLEntry)i$.next();
    }
    return list;
  }

  public static List<BigInteger> getCRLSerialNumbers(X509CRL crl) throws Exception {
    List list = new ArrayList();
    if (crl != null)
    {
      X509CRLEntry entry;
      for (Iterator i$ = crl.getRevokedCertificates().iterator(); i$.hasNext(); list.add(entry.getSerialNumber())) entry = (X509CRLEntry)i$.next();
    }
    return list;
  }

  public static boolean isRevoked(BigInteger serialNum, InputStream inCRL) throws Exception
  {
    List serialNumbers = getCRLSerialNumbers(inCRL);
    return serialNumbers.contains(serialNum);
  }

  public static boolean isRevoked(Certificate cert, X509CRL crl) throws Exception
  {
    List serialNumbers = getCRLSerialNumbers(crl);
    X509Certificate xcert = (X509Certificate)cert;
    return serialNumbers.contains(xcert.getSerialNumber());
  }

  public static X509CRLEntry getRevoked(Certificate cert, X509CRL crl) throws Exception
  {
    X509Certificate xcert = (X509Certificate)cert;
    BigInteger cNum = xcert.getSerialNumber();
    if (crl != null) {
      for (X509CRLEntry entry : crl.getRevokedCertificates()) {
        if (cNum.equals(entry.getSerialNumber())) return entry;
      }
    }
    return null;
  }

  public static String getReasonCode(X509CRLEntry entry) {
    return null;
  }

  public static Date[] getValidityInterval(Certificate cert)
  {
    Date[] interval = new Date[2];
    X509Certificate xcert = (X509Certificate)cert;

    interval[0] = xcert.getNotBefore();
    interval[1] = xcert.getNotAfter();

    return interval;
  }
}

/* Location:           C:\Users\Kirill\Desktop\esign\esign\esign.jar
 * Qualified Name:     com.asteros.crypto.CertificateUtil
 * JD-Core Version:    0.6.0
 */